package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.service.BtoOrderService;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.service.BtoUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author 王小波
 * @description: 勘察阶段任务处理器
 * 勘察有：业务踏勘、技术勘察 两个任务
 * @date 2023/7/21 17:09
 */

@Slf4j
@Component
public class KanChaStageHandler implements StageHandler {
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private BtoOrderService btoOrderService;
    @Autowired
    private TransactionTemplate transactionTemplate;

    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId(), BizTaskStateEnum.WAITING_FILLED.getStateId());

    }

    @Override
    public void updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        if (BizTaskEnum.TASK_TA_KAN.getTaskId().equals(taskId)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId(),
                    BizTaskStateEnum.WAITING_APPROVAL_INNER.getStateId(),
                    BizTaskStateEnum.WAITING_FILLED.getStateId(),
                    BizTaskStateEnum.APPROVAL_REJECT.getStateId(),
                    BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId() ,
                    BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId());
        } else if (BizTaskEnum.TASK_JSKC.getTaskId().equals(taskId)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_JSKC.getTaskId(),
                    BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(),
                    BizTaskStateEnum.WAITING_FILLED.getStateId(),
                    BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId() ,
                    BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId());
        }
    }
    @Transactional
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId, String approvalType) {
        if (Constants.APPROVAL_INNER.equals(approvalType)) {
            //业务踏勘审核通过，需要提前提交事务，避免下面从内审通过 -> 待博通审核会报错
            transactionTemplate.executeWithoutResult(transactionStatus -> {
                btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId(),
                        BizTaskStateEnum.APPROVAL_PASS.getStateId(),
                        BizTaskStateEnum.WAITING_APPROVAL_INNER.getStateId(),
                        true);
            });

            //业务踏勘审核通过，需要博通二审
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId(),
                    BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(),
                    BizTaskStateEnum.APPROVAL_PASS.getStateId(),
                    true);
            //创建技术勘察-待指派
            btoOrderStateService.createOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_JSKC.getTaskId(),
                    BizTaskStateEnum.WAITING_APPOINT.getStateId());
        } else if (Constants.APPROVAL_BTO.equals(approvalType)) {
            //踏勘审核通过，需要博通二审
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), taskId,
                    BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId(), BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(), true);
            //推送越秀-进件风险审查
            btoOrderService.pushSyncProjectLeasePreApproval(orderId);
        }


    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId, String approvalType  , Object rejectInfo) {
        //越秀拒绝
        if(Constants.APPROVAL_LEASE.equals(approvalType)){
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId(),
                    BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId(),
                    BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId(),
                    true);
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_JSKC.getTaskId(),
                    BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId(),
                    BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId(),
                    true);
        }
        //内审、博通拒绝
        else {
            if (BizTaskEnum.TASK_TA_KAN.getTaskId().equals(taskId)) {
                if (Constants.APPROVAL_INNER.equals(approvalType)) {
                    btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId(),
                            BizTaskStateEnum.APPROVAL_REJECT.getStateId(),
                            BizTaskStateEnum.WAITING_APPROVAL_INNER.getStateId(), true);
                } else if (Constants.APPROVAL_BTO.equals(approvalType)) {
                    btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId(),
                            BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId(),
                            BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(),
                            true);
                }
            } else if (BizTaskEnum.TASK_JSKC.getTaskId().equals(taskId)) {
                btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_JSKC.getTaskId(),
                        BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId(), BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(), true);
            }
        }


    }

    /**
     * 技术指派完成-变为待填写
     */
    public void updateWaitingAppointToFilled(String orderId) {
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_JSKC.getTaskId(),
                BizTaskStateEnum.WAITING_FILLED.getStateId(),
                BizTaskStateEnum.WAITING_APPOINT.getStateId(),
                BizTaskStateEnum.WAITING_FILLED.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId());
    }
}
